package com.example.chapterspringbootandmybatis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.chapterspringbootandmybatis.entity.Book;
import com.example.chapterspringbootandmybatis.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;

    /*
     * 查询全部信息
     * */
    @GetMapping
    public String getByList(Model model) {
        List<Book> list = bookService.list();
        model.addAttribute("list", list);
        return "book";
    }


    /*
     * 条件查询
     * */
    @PostMapping
    public String getBySelect(Model model, Book book) {
        List<Book> books = bookService.getBySelect(book);
        model.addAttribute("name", book.getName());
        model.addAttribute("author", book.getAuthor());
        model.addAttribute("category", book.getCategory());
        model.addAttribute("books", books);
        return "book";
    }


    /*
     * 分页查询
     * */
    @GetMapping("/page")
    public String page(Model model,
                       @RequestParam(defaultValue = "1") Integer pageNum){
        IPage list = bookService.getPage(pageNum);
        model.addAttribute("page",list);
        return "book";
    }
}
